// 语言配置
const translations = {
    'zh-CN': {
        // 导航
        'nav_home': '首页',
        'nav_about': '关于我',
        'nav_projects': '项目',
        'nav_contact': '联系',
        
        // 首页
        'hero_title': '欢迎来到我的作品集',
        'hero_subtitle': '我是一名充满激情的开发者',
        'view_projects': '查看项目',
        
        // 关于我
        'about_title': '关于我',
        'personal_intro': '个人简介',
        'about_description': '我是一名Python Developer，擅长消息中间件、实时数据处理、高可用架构设计。喜欢Rust、幸运星、孤独摇滚、摇曳露营。',
        'about_description1': '我是一名充满激情的全栈开发工程师，拥有5年的开发经验。我热爱技术，喜欢探索新的技术栈和解决方案。',
        'about_description2': '我的目标是通过技术创造价值，为用户提供优秀的产品体验。我相信好的代码不仅要功能完善，还要易于维护和扩展。',
        'about_experience': '专业经历',
        'about_experience_years': '5年+ 开发经验',
        'about_projects': '完成项目',
        'about_projects_count': '50+ 项目',
        'about_satisfaction': '客户满意度',
        'about_satisfaction_rate': '98% 好评',
        'skills_title': '技能专长',
        'expert_level': '专家',
        'proficient_level': '熟练',
        'intermediate_level': '中级',
        'professional_experience': '专业经历',
        'personal_achievements': '个人成就',
        'tsinghua_graduate': '清华大学优秀毕业生',
        'microsoft_mvp': '微软MVP',
        'huawei_certification': '华为开发者认证',
        
        // 项目
        'projects_title': '我的项目',
        'project_one': '项目一',
        'project_one_desc': '这是一个简短的项目描述，展示项目的主要功能和特点。',
        'project_two': '项目二',
        'project_two_desc': '这是另一个项目的描述，突出显示不同的技术和技能。',
        'project_three': '项目三',
        'project_three_desc': '第三个项目展示了我的全栈开发能力和创新思维。',
        'projects.project1.title': '电商平台',
        'projects.project1.description': '一个功能完整的电商平台，包含用户管理、商品展示、购物车、支付等功能。',
        'projects.project2.title': '任务管理系统',
        'projects.project2.description': '一个现代化的任务管理应用，支持团队协作、进度跟踪、文件共享等功能。',
        'projects.project3.title': '数据可视化平台',
        'projects.project3.description': '一个强大的数据可视化平台，支持多种图表类型，实时数据更新。',
        'view_demo': '查看演示',
        'view_code': '查看代码',
        
        // 联系
        'contact_title': '联系我',
        'contact_intro': '如果您对我的工作感兴趣，或者想要合作，请随时联系我！',
        'email_label': '邮箱：',
        'phone_label': '电话：',
        'address_label': '地址：',
        'social_media': '社交媒体',
        'personal_website': '个人网页',
        'email_contact': '邮箱联系',
        'name_label': '姓名：',
        'email_input_label': '邮箱：',
        'message_label': '留言：',
        'message_placeholder': '请输入您的留言内容...',
        'send_message': '发送消息',
        'form_success': '感谢您的消息！我会尽快回复您。',
        'form_error_required': '请填写所有必填字段！',
        'form_error_email': '请输入有效的邮箱地址！',
        
        // 页脚
        'footer_copyright': '© 2024 我的作品集. 保留所有权利.',
        'footer_gitee': 'Gitee',
        'footer_email': '邮箱',
        'footer_wechat': '微信',
        
        // 语言切换
        'language_switch': '语言',
        
        // 深色模式
        'dark_mode': '深色模式',
        'light_mode': '浅色模式',
        'theme_switched_to_dark': '已切换到深色模式',
        'theme_switched_to_light': '已切换到浅色模式',
        
        // 技能名称
        'skills.frontend_dev': '前端开发',
        'skills.backend_dev': '后端开发',
        'skills.database_design': '数据库设计',
        'skills.ui_ux_design': 'UI/UX设计',
        'skills.project_management': '项目管理',
        
        // 技术标签
        'tech.react': 'React',
        'tech.nodejs': 'Node.js',
        'tech.mongodb': 'MongoDB',
        'tech.vuejs': 'Vue.js',
        'tech.express': 'Express',
        'tech.mysql': 'MySQL',
        'tech.d3js': 'D3.js',
        'tech.python': 'Python',
        'tech.postgresql': 'PostgreSQL'
    },
    'en': {
        // 导航
        'nav_home': 'Home',
        'nav_about': 'About',
        'nav_projects': 'Projects',
        'nav_contact': 'Contact',
        
        // 首页
        'hero_title': 'Welcome to My Portfolio',
        'hero_subtitle': 'I am a passionate developer',
        'view_projects': 'View Projects',
        
        // 关于我
        'about_title': 'About Me',
        'personal_intro': 'Personal Introduction',
        'about_description': 'I am a Python Developer, specializing in message middleware, real-time data processing, and high-availability architecture design. I like Rust, Lucky Star, Bocchi the Rock, and Laid-Back Camp.',
        'about_description1': 'I am a passionate full-stack developer with 5 years of development experience. I love technology and enjoy exploring new tech stacks and solutions.',
        'about_description2': 'My goal is to create value through technology and provide excellent product experiences for users. I believe good code should not only be functionally complete but also easy to maintain and extend.',
        'about_experience': 'Professional Experience',
        'about_experience_years': '5+ Years Development Experience',
        'about_projects': 'Completed Projects',
        'about_projects_count': '50+ Projects',
        'about_satisfaction': 'Client Satisfaction',
        'about_satisfaction_rate': '98% Positive Reviews',
        'skills_title': 'Skills & Expertise',
        'expert_level': 'Expert',
        'proficient_level': 'Proficient',
        'intermediate_level': 'Intermediate',
        'professional_experience': 'Professional Experience',
        'personal_achievements': 'Personal Achievements',
        'tsinghua_graduate': 'Tsinghua University Outstanding Graduate',
        'microsoft_mvp': 'Microsoft MVP',
        'huawei_certification': 'Huawei Developer Certification',
        
        // 项目
        'projects_title': 'My Projects',
        'project_one': 'Project One',
        'project_one_desc': 'This is a brief project description showcasing the main features and characteristics.',
        'project_two': 'Project Two',
        'project_two_desc': 'This is another project description highlighting different technologies and skills.',
        'project_three': 'Project Three',
        'project_three_desc': 'The third project demonstrates my full-stack development capabilities and innovative thinking.',
        'projects.project1.title': 'E-commerce Platform',
        'projects.project1.description': 'A fully functional e-commerce platform with user management, product display, shopping cart, payment and other features.',
        'projects.project2.title': 'Task Management System',
        'projects.project2.description': 'A modern task management application supporting team collaboration, progress tracking, file sharing and other features.',
        'projects.project3.title': 'Data Visualization Platform',
        'projects.project3.description': 'A powerful data visualization platform supporting multiple chart types and real-time data updates.',
        'view_demo': 'View Demo',
        'view_code': 'View Code',
        
        // 联系
        'contact_title': 'Contact Me',
        'contact_intro': 'If you are interested in my work or would like to collaborate, please feel free to contact me!',
        'email_label': 'Email:',
        'phone_label': 'Phone:',
        'address_label': 'Address:',
        'social_media': 'Social Media',
        'personal_website': 'Personal Website',
        'email_contact': 'Email Contact',
        'name_label': 'Name:',
        'email_input_label': 'Email:',
        'message_label': 'Message:',
        'message_placeholder': 'Please enter your message...',
        'send_message': 'Send Message',
        'form_success': 'Thank you for your message! I will reply as soon as possible.',
        'form_error_required': 'Please fill in all required fields!',
        'form_error_email': 'Please enter a valid email address!',
        
        // 页脚
        'footer_copyright': '© 2024 My Portfolio. All rights reserved.',
        'footer_gitee': 'Gitee',
        'footer_email': 'Email',
        'footer_wechat': 'WeChat',
        
        // 语言切换
        'language_switch': 'Language',
        
        // 深色模式
        'dark_mode': 'Dark Mode',
        'light_mode': 'Light Mode',
        'theme_switched_to_dark': 'Switched to dark mode',
        'theme_switched_to_light': 'Switched to light mode',
        
        // 技能名称
        'skills.frontend_dev': 'Frontend Development',
        'skills.backend_dev': 'Backend Development',
        'skills.database_design': 'Database Design',
        'skills.ui_ux_design': 'UI/UX Design',
        'skills.project_management': 'Project Management',
        
        // 技术标签
        'tech.react': 'React',
        'tech.nodejs': 'Node.js',
        'tech.mongodb': 'MongoDB',
        'tech.vuejs': 'Vue.js',
        'tech.express': 'Express',
        'tech.mysql': 'MySQL',
        'tech.d3js': 'D3.js',
        'tech.python': 'Python',
        'tech.postgresql': 'PostgreSQL'
    }
};

// 语言管理器
class LanguageManager {
    constructor() {
        this.currentLanguage = localStorage.getItem('language') || 'zh-CN';
        this.init();
    }
    
    init() {
        this.updateLanguageSelector();
        this.applyTranslations();
        this.bindEvents();
    }
    
    // 绑定事件
    bindEvents() {
        const languageSelector = document.getElementById('language-selector');
        if (languageSelector) {
            languageSelector.addEventListener('change', (e) => {
                this.setLanguage(e.target.value);
            });
        }
    }
    
    // 设置语言
    setLanguage(language) {
        if (translations[language]) {
            this.currentLanguage = language;
            localStorage.setItem('language', language);
            this.applyTranslations();
            this.updateLanguageSelector();
            
            // 更新HTML lang属性
            document.documentElement.lang = language;
        }
    }
    
    // 获取翻译
    getTranslation(key) {
        return translations[this.currentLanguage][key] || key;
    }
    
    // 获取当前语言
    getCurrentLanguage() {
        return this.currentLanguage;
    }
    
    // 应用翻译
    applyTranslations() {
        // 导航
        this.translateElement('.nav-link[href="#home"]', 'nav_home');
        this.translateElement('.nav-link[href="#about"]', 'nav_about');
        this.translateElement('.nav-link[href="#projects"]', 'nav_projects');
        this.translateElement('.nav-link[href="#contact"]', 'nav_contact');
        
        // 首页
        this.translateElement('.hero-title', 'hero_title');
        this.translateElement('.hero-subtitle', 'hero_subtitle');
        this.translateElement('#view-projects', 'view_projects');
        
        // 关于我
        this.translateElement('#about h2', 'about_title');
        this.translateElement('.intro-section h3', 'personal_intro');
        this.translateElement('.intro-section p', 'about_description');
        this.translateElement('.about-text p[data-i18n="about.description1"]', 'about_description1');
        this.translateElement('.about-text p[data-i18n="about.description2"]', 'about_description2');
        
        // 统计部分
        this.translateElement('.stat-item h3[data-i18n="about.experience"]', 'about_experience');
        this.translateElement('.stat-item p[data-i18n="about.experience_years"]', 'about_experience_years');
        this.translateElement('.stat-item h3[data-i18n="about.projects"]', 'about_projects');
        this.translateElement('.stat-item p[data-i18n="about.projects_count"]', 'about_projects_count');
        this.translateElement('.stat-item h3[data-i18n="about.satisfaction"]', 'about_satisfaction');
        this.translateElement('.stat-item p[data-i18n="about.satisfaction_rate"]', 'about_satisfaction_rate');
        
        this.translateElement('.skills h3', 'skills_title');
        this.translateElement('.timeline h3', 'professional_experience');
        this.translateElement('.achievements h3', 'personal_achievements');
        
        // 技能名称
        this.translateElements('.skill-header span:first-child', {
            '前端开发': 'skills.frontend_dev',
            '后端开发': 'skills.backend_dev',
            '数据库设计': 'skills.database_design',
            'UI/UX设计': 'skills.ui_ux_design',
            '项目管理': 'skills.project_management'
        });
        
        // 技能等级
        this.translateElements('.skill-proficiency', {
            '专家': 'expert_level',
            '熟练': 'proficient_level',
            '中级': 'intermediate_level'
        });
        
        // 成就
        this.translateElements('.achievement-item h4', {
            '清华大学优秀毕业生': 'tsinghua_graduate',
            '微软MVP': 'microsoft_mvp',
            '华为开发者认证': 'huawei_certification'
        });
        
        // 项目
        this.translateElement('#projects h2', 'projects_title');
        this.translateElement('.project-card[data-project="project1"] h3', 'projects.project1.title');
        this.translateElement('.project-card[data-project="project1"] p', 'projects.project1.description');
        this.translateElement('.project-card[data-project="project2"] h3', 'projects.project2.title');
        this.translateElement('.project-card[data-project="project2"] p', 'projects.project2.description');
        this.translateElement('.project-card[data-project="project3"] h3', 'projects.project3.title');
        this.translateElement('.project-card[data-project="project3"] p', 'projects.project3.description');
        this.translateElements('.project-link', {
            '在线演示': 'view_demo',
            '源代码': 'view_code'
        });
        
        // 技术标签
        this.translateElements('.tech-tag', {
            'React': 'tech.react',
            'Node.js': 'tech.nodejs',
            'MongoDB': 'tech.mongodb',
            'Vue.js': 'tech.vuejs',
            'Express': 'tech.express',
            'MySQL': 'tech.mysql',
            'D3.js': 'tech.d3js',
            'Python': 'tech.python',
            'PostgreSQL': 'tech.postgresql'
        });
        
        // 联系
        this.translateElement('#contact h2', 'contact_title');
        this.translateElement('.contact-info p', 'contact_intro');
        this.translateElement('.contact-item strong', 'email_label', 0);
        this.translateElement('.contact-item strong', 'phone_label', 1);
        this.translateElement('.contact-item strong', 'address_label', 2);
        this.translateElement('.social-media h4', 'social_media');
        this.translateElements('.social-link-vertical', {
            '个人网页': 'personal_website',
            '邮箱联系': 'email_contact'
        });
        
        // 表单
        this.translateElement('.form-group label[for="name"]', 'name_label');
        this.translateElement('.form-group label[for="email-input"]', 'email_input_label');
        this.translateElement('.form-group label[for="message"]', 'message_label');
        this.translateElement('#message', 'message_placeholder', null, 'placeholder');
        this.translateElement('.submit-button', 'send_message');
        
        // 页脚
        this.translateElement('footer p', 'footer_copyright');
        this.translateElements('.social-link', {
            'Gitee': 'footer_gitee',
            '邮箱': 'footer_email',
            '微信': 'footer_wechat'
        });
        
        // 语言切换器标签
        this.translateElement('.language-label', 'language_switch');
        
        // 更新深色模式切换按钮文本
        this.updateThemeToggleButton();
    }
    
    // 更新深色模式切换按钮
    updateThemeToggleButton() {
        const themeToggle = document.getElementById('theme-toggle');
        if (themeToggle) {
            const currentTheme = document.documentElement.getAttribute('data-theme');
            const isDark = currentTheme === 'dark';
            const icon = isDark ? '☀️' : '🌙';
            const text = isDark ? this.getTranslation('light_mode') : this.getTranslation('dark_mode');
            themeToggle.innerHTML = `${icon} ${text}`;
            themeToggle.setAttribute('aria-label', `切换到${text}`);
        }
    }
    
    // 翻译单个元素
    translateElement(selector, translationKey, index = 0, attribute = 'textContent') {
        const elements = document.querySelectorAll(selector);
        if (elements.length > index) {
            const element = elements[index];
            const translation = this.getTranslation(translationKey);
            
            if (attribute === 'textContent') {
                element.textContent = translation;
            } else if (attribute === 'placeholder') {
                if (element) {
                    element.placeholder = translation;
                }
            }
        }
    }
    
    // 翻译多个元素
    translateElements(selector, translationMap) {
        const elements = document.querySelectorAll(selector);
        elements.forEach(element => {
            const originalText = element.textContent || element.innerText;
            if (translationMap[originalText]) {
                const translation = this.getTranslation(translationMap[originalText]);
                element.textContent = translation;
            }
        });
    }
    
    // 更新语言选择器
    updateLanguageSelector() {
        const languageSelector = document.getElementById('language-selector');
        if (languageSelector) {
            languageSelector.value = this.currentLanguage;
        }
    }
}

// 初始化语言管理器 - 移除声明，使用main.js中的全局变量
document.addEventListener('DOMContentLoaded', function() {
    if (typeof languageManager === 'undefined' || !languageManager) {
        languageManager = new LanguageManager();
    }
});

// 全局函数供其他脚本使用
function setLanguage(language) {
    if (languageManager) {
        languageManager.setLanguage(language);
    }
}

function getTranslation(key) {
    if (languageManager) {
        return languageManager.getTranslation(key);
    }
    return key;
}